package com.ccl.bigevent.service.impl;

import com.ccl.bigevent.mapper.ArticleMapper;
import com.ccl.bigevent.pojo.Article;
import com.ccl.bigevent.pojo.PageBean;
import com.ccl.bigevent.service.ArticleService;
import com.ccl.bigevent.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Title: ArticleServiceImpl
 * @Author ccl
 * @Package IntelliJ IDEA
 * @Date 2024/10/26 10:21
 * @description:
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {

        PageBean<Article> pageBean = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        List<Article> list = articleMapper.list(userId, categoryId, state);

        Page<Article> page = (Page<Article>) list;

        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());

        return pageBean;

    }

    @Override
    public void add(Article article) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
    }

}
